<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2021/3/15
 * Time: 15:33
 */

namespace backend\modules\video\controllers;

use common\models\Author\Author;
use common\models\video\Video;
use Yii;
use common\models\base\SearchModel;
use common\traits\Curd;
use common\enums\StatusEnum;
use backend\controllers\BaseController;

class VideoController extends BaseController
{
    use Curd;

    /**
     * @var video
     */
    public $modelClass = video::class;

    /**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['name'], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query
            ->innerJoinWith('author','author.id = video.author_id')
            ->andWhere(['>=', 'rf_video.status', StatusEnum::DISABLED]);
        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }
    public function actionEdit()
    {
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);
        $cate = new Author();
        $author = $cate->find()->where(['status' =>'1'])->all();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        }

        return $this->render($this->action->id, [
            'model' => $model,
            'author' => $author,
        ]);
    }
}